package national_holiday.array_practice;

import java.util.Arrays;

public class add_function {
    int[] a;
    int[] b;
    int[] result;
    public add_function(int[] a,int[] b,int[] result) {
        this.a=a;
        this.b=b;
        this.result=result;
    }

    /**
     * 计算两个数相乘出来的结果
     * @param a      大数字的被乘数
     * @param b      乘数
     */
    public int[] multiply(int[] a, int[] b,int index) {
        int[] result=new int[a.length];
//        用于两个数字分别相乘
        for (int i = 0; i < result.length; i++) {
            int r = a[i] * b[index];
//        相乘的数的十位的数字
            int decade = r / 10;
//        相乘出来的数的个位的数字
            int unit = r - (r / 10) * 10;
            if (i <= 0) {
                if (i == result.length - 1) {
                    result[i] = unit;
                } else {
                    result[i] = unit;
                    result[i + 1] = decade;
                }

            } else {
                if (i == result.length - 1) {
                    int oldDecade = (result[i]);
                    result[i] = unit + oldDecade;
                    if (result[i] / 10 != 0) {
                        int num = result[i];
                        result[i] = num - (num / 10) * 10;
                    }
                } else {
                    int oldDecade = (result[i]);
                    result[i] = unit + oldDecade;
                    result[i + 1] = decade;
                    if (result[i] / 10 != 0) {
                        int num = result[i];
                        result[i] = num - (num / 10) * 10;
                        result[i + 1] = decade + num / 10;
                    }
                }

            }

        }
//        System.out.println(Arrays.toString(result));
        return result;
    }

    public void add(){
        int[] num=multiply(a,b,0);
        int[] num1=new int[num.length];
        for (int i=1;i< num.length;i++){
            num1[i]=num[i-1];
        }
        int[] num2=multiply(a,b,1);
        int[] fruit=new int[num1.length+1];
        for (int i=0;i< fruit.length-1;i++){
            fruit[i]=num1[i]+num2[i];
            int decade=fruit[i]/10;
            int unit=fruit[i]-(fruit[i]/10)*10;
            if (fruit[i]<10){
                if (i== fruit.length-1){
                    fruit[i]=unit;
                }else {
                    fruit[i]=unit;
                    fruit[i+1]=decade;
                }

            }else {
                if (i== fruit.length-1){
                    fruit[i]=unit;
                }else {
                    int oldDecade=fruit[i-1]/10;
                    fruit[i]=unit;
                    fruit[i+1]=decade+oldDecade;
                }
            }
        }
        System.out.println(Arrays.toString(fruit));
    }
}
